//
//  LeftButton.m
//  MixDrinks
//
//  Created by admin on 2016/12/27.
//  Copyright © 2016年 Zhongshan Camry Electronic Company Limited. All rights reserved.
//
#define btnH4 4
#define btnH3 3
#define btnHA4 200
#define btnHB 100
#import "LeftButton.h"

@interface LeftButton ()
@property (nonatomic, weak) UIButton *selectedButton;
@end

@implementation LeftButton

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setupBtnWithIcon:@"" title:@"酒单库" backgroundColor:colorA(26, 26, 26, 255)];
        [self setupBtnWithIcon:@"" title:@"自定义酒单" backgroundColor:colorA(26, 26, 26, 255)];
        [self setupBtnWithIcon:@"" title:@"称重" backgroundColor:colorA(26, 26, 26, 255)];
        [self setupBtnWithIcon:@"" title:@"单位设置" backgroundColor:colorA(26, 26, 26, 255)];
        [self setupBtnWithIcon:@"" title:@"意见反馈" backgroundColor:colorA(26, 26, 26, 255)];
        [self setupBtnWithIcon:@"" title:@"版本信息" backgroundColor:colorA(26, 26, 26, 255)];
        [self setupBtnWithIcon:@"" title:@"关于我们" backgroundColor:colorA(26, 26, 26, 255)];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    //设置frame
    NSUInteger btnCount = self.subviews.count;
    CGFloat btnW = self.width;
    CGFloat btnH = btnHA4 / btnH4;
    CGFloat btnBH = btnHB / btnH3;
    for (int i = 0; i < 4; i++) {
        UIButton *btn = self.subviews[i];
        btn.width = btnW;
        btn.height = btnH;
        btn.y = btnH * i;
    }
    for (NSUInteger i = 4; i < btnCount; i++) {
        UIButton *btn = self.subviews[i];
        btn.width = btnW;
        btn.height = btnBH;
        btn.y = (self.height - (btnHA4 + btnHB) * Width_Scale) + btnBH * i;
    }
}

- (UIButton *)setupBtnWithIcon:(NSString *)icon title:(NSString *)title backgroundColor:(UIColor *)bgColor {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = self.subviews.count;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    //设置文字和图片
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:colorA(153, 153, 153, 255) forState:UIControlStateNormal];
    if (btn.tag < 4) {
        [btn setTitleColor:colorA(255, 255, 255, 255) forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageWithColor:bgColor] forState:UIControlStateSelected];
    }
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    //设置背景
    btn.adjustsImageWhenHighlighted = NO;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    return btn;
}

- (void)setDelegate:(id<leftButtonDelegate>)delegate {
    _delegate = delegate;
    [self btnClick:[self.subviews firstObject]];
}

- (void)btnClick:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(leftButton:selectFromeIndex:ToIndex:)]) {
        [self.delegate leftButton:self selectFromeIndex:self.selectedButton.tag ToIndex:btn.tag];
    }
    self.selectedButton.selected = NO;
    btn.selected = YES;
    self.selectedButton = btn;
}

@end
